package dse.messages.singleversion;

import dse.*;
import dse.messages.*;

public class RequestShareForeignMessageHandler
extends MessageSender
implements MessageHandler
{
	private DataObject dobj;
	private int shareIndex;
	private MessagePurpose purpose;
	private ShareDownloader shareDownloader;
	


	public RequestShareForeignMessageHandler (
			Experiment experiment, 
			DataObject dobj, 
			int shareIndex,
			MessagePurpose purpose,
			ShareDownloader shareDownloader
			)
	{
		super(experiment);
		
		this.dobj = dobj;
		this.shareIndex = shareIndex;
		this.purpose = purpose;
		this.shareDownloader = shareDownloader;
	}
	
	
	
	public boolean acceptMessage (
			Message message
			)
	{
		return true;
	}
	
	public void messageReceived (
			ExperimentRecorder recorder, 
			Message message,
			double timeLeft
			)
	{
		recorder.debug(this, "msgRcvd: " + message, timeLeft);
		if (message.messageType() != MessageType.RequestShareForeign) {
			recorder.warn(
					"RequestShareForeignMessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}
				
		// What do we know about the share?
		Node receiver = message.to();
		
		// Does the reciever have the share?
		if (receiver.isHosting(dobj, shareIndex) 
				//||receiver.isCaching(dobj, shareIndex)
				)
		{
			// Then we need to send a new message BACK (thats why to and from
			// are reversed!
			Message m = super.createShareDataTransferMessage(
					receiver, message.from(), dobj, shareIndex, purpose, 
					shareDownloader
					);
			recorder.debug(this, "new msg: " + m, timeLeft);
			//experiment.messageQueue().enqueueMessageInPeriod(m, timeLeft);
		} else {
			// Then we have to tell them that we dont have it!
			Message m = super.createNegativeRequestShareForeignResponseMessage(
					receiver, message.from(), dobj, shareIndex, purpose,
					shareDownloader
					);
			recorder.debug(this, "new msg: " + m, timeLeft);
			//experiment.messageQueue().enqueueMessageInPeriod(m, timeLeft);
			
			/*if(receiver.isUp())
			{
				Vector<Cluster> clusters = (Vector<Cluster>)experiment.modules().assigner().assign(dobj, 
						experiment.network().clusters(), experiment.network().maxClusterId());
				if(clusters.contains(experiment.network().lookupCluster(receiver.clusterId())))	
				{
					if(!experiment.nodesInRecentDataTransactionsList.containsKey(receiver))
					{
						int transId = experiment.getNewTransactionId();
						Transaction trans = null;
						
						if (experiment.configuration().getTestType() == TestType.Mixed)
							trans = new GetOneShareTransaction(experiment, transId, receiver, dobj, shareIndex);
						experiment.transactions.add(trans);
						experiment.nodesInRecentDataTransactionsList.put(receiver, Experiment.currentPeriod);
						trans.start(recorder, timeLeft);
					}
				}
			}*/
		}		
	}
	

	
	public void messageInterupted (
			ExperimentRecorder recorder, 
			Message message,
			double timeLeft
			)
	{
		recorder.debug(this, "msgIntrpt: " + message, timeLeft);
		shareDownloader.shareDownloadFailed(
				recorder, dobj, shareIndex, timeLeft
				);
	}

}
